<template>
  <!-- 维护作业-绩优代理人维护页面 -->
  <div class="fillcontain">
    <el-form :model="searchParams" :rules="searchRules" ref="searchForm" label-width="100px" class="search_container">
      <el-row>
        <el-col :span="6">
          <el-form-item label="公司" prop="chdrcoy">
            <el-input clearable v-model="searchParams.chdrcoy" maxlength="1" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="营销员代码" prop="agntnum">
            <el-input clearable v-model="searchParams.agntnum" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6" class="text_center">
          <el-button type="primary" icon="el-icon-search" @click="subGetList"></el-button>
          <el-button type="primary" plain icon="el-icon-refresh" @click="resetSearch"></el-button>
        </el-col>
      </el-row>
    </el-form>
    <div class="contain">
      <el-row class="bgfff pb15">
        <el-col :span="24" class="pt5">
          <el-button type="danger" plain @click="addClick">新增</el-button>
        </el-col>
      </el-row>
      <!-- 查询结果 -->
      <div class="table_container">
        <el-table
            v-loading="loading"
            :data="tableData"
            highlight-current-row
            header-cell-class-name="table-header-class"
            style="width:100%">
          <el-table-column
              min-width="120"
              property="chdrcoy"
              label="公司"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              min-width="120"
              property="agntnum"
              label="营销员代码"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              min-width="120"
              property="agntname"
              label="营销员姓名"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              min-width="120"
              property="agcyname"
              label="所属营管处"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              min-width="120"
              property="vldstrdate"
              label="有效开始时间"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              min-width="120"
              property="vldenddate"
              label="有效结束时间"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              min-width="120"
              property="strdate"
              label="开始时间"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              min-width="120"
              property="enddate"
              label="结束时间"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              min-width="120"
              property="crtuser"
              label="录入人员"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              min-width="120"
              property="chaguser"
              label="结束人员"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              min-width="120"
              property="branch"
              label="Branch"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              fixed="right"
              min-width="120"
              label="操作"
              align='center'>
            <template slot-scope="scope">
              <span class="operationIcon" @click="modifyClick(scope.row)">
                <el-tooltip class="item" effect="light" content="修改" placement="top">
                  <i class="el-icon-edit"></i>
                </el-tooltip>
              </span>
              <span class="operationIcon ml5" @click="deleteData(scope.row)">
                <el-tooltip class="item" effect="light" content="删除" placement="top">
                  <i class="el-icon-delete"></i>
                </el-tooltip>
              </span>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-col :span="24">
            <div class="pagination">
              <el-pagination
                  background
                  v-if='paginations.total > 0'
                  :page-sizes="paginations.pageSizes"
                  :page-size="paginations.pageSize"
                  :layout="paginations.layout"
                  :total="paginations.total"
                  :current-page='paginations.pageIndex'
                  @current-change='handleCurrentChange'
                  @size-change='handleSizeChange'>
              </el-pagination>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <!-- 弹框 修改-->
    <el-dialog
        :title="title_O"
        :visible.sync="showDialog"
        width="50%"
        center>
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-row>
          <el-col :span="12" class="common-box">
            <el-form-item label="公司" prop="chdrcoy">
              <el-input clearable v-model="form.chdrcoy" placeholder="请输入" maxlength='1'
                        :readonly="form.method == 'view'"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="营销员代码" prop="agntnum">
              <el-input clearable v-model="form.agntnum" @blur="checkAgntnum"
                        :disabled="form.method != 'add'" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="营销员姓名" prop="agntname">
              <el-input clearable v-model="form.agntname" disabled placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="所属营管处" prop="agcyname">
              <el-input clearable v-model="form.agcyname" disabled placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="有效开始时间" prop="vldstrdate">
              <el-input clearable v-model="form.vldstrdate" :readonly="form.method == 'view'" maxlength='8'
                        placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="有效结束时间" prop="vldenddate">
              <el-input clearable v-model="form.vldenddate" :readonly="form.method == 'view'" maxlength='8'
                        placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="开始时间" prop="strdate">
              <el-input clearable v-model="form.strdate" :readonly="form.method == 'view'" maxlength='8'
                        placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="结束时间" prop="enddate">
              <el-input clearable v-model="form.enddate" :readonly="form.method == 'view'" maxlength='8'
                        placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="录入人员" prop="crtuser">
              <el-input clearable v-model="form.crtuser" :readonly="form.method == 'view'" maxlength="9"
                        placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="结束人员" prop="chaguser">
              <el-input clearable v-model="form.chaguser" :readonly="form.method == 'view'" maxlength="9"
                        placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="Branch" prop="branch">
              <el-input clearable v-model="form.branch" :readonly="form.method == 'view'" maxlength="2"
                        placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="saveData" class="Defaultbutton">保 存</el-button>
                <el-button @click="showDialog = false">返 回</el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
  import { mapState, mapGetters } from 'vuex'
  import constant from '@/utils/constant';
  import * as api from '@/api/setting/maintain/superAgentMaintenance.js';
  import validRules from "@/utils/validRules";

  export default {
    data() {
      var containChinese = (rule, value, callback) => {
        var that = this;
        let CHNReg = /[\u4e00-\u9fa5]/;//汉字的正则表达式
        if (CHNReg.test(value)) {
          callback(new Error('不能为中文!'));
        } else {
          callback();
        }
      };
      var isNumber = (rule, value, callback) => {
        var that = this;
        if (isNaN(value)) {
          callback(new Error('只能是数字!'));
        } else {
          callback();
        }
      };
      return {
        valueDate: '',
        showDialog: false,//默认隐藏 修改弹框
        title_O: '绩优代理人维护',
        // 查询条件
        rules: {
          chdrcoy: [
            { required: true, message: '公司不能为空!', trigger: 'blur' },
            { validator: containChinese, trigger: 'blur' }
          ],
          agntnum: [
            { required: true, message: '绩优营销员代码必须输入!', trigger: 'blur' },
            // { validator: containChinese, trigger: 'blur'}
          ],
          agntname: [
            { required: true, message: '营销员姓名不能为空!', trigger: 'blur' },
            // { validator: containChinese, trigger: 'blur'}
          ],
          branch: [
            { required: true, message: 'branch不能为空!', trigger: 'blur' },
            { validator: containChinese, trigger: 'blur' }
          ],
          crtuser: [
            { validator: containChinese, trigger: 'blur' }
          ],
          chaguser: [
            { validator: containChinese, trigger: 'blur' }
          ],
          vldstrdate: [
            { validator: validRules.isNumber, message: '有效开始时间只能是数字', trigger: 'blur' }
          ],
          vldenddate: [
            { validator: validRules.isNumber, message: '有效结束时间只能是数字', trigger: 'blur' }
          ],
          strdate: [
            { validator: validRules.isNumber, message: '开始时间只能是数字', trigger: 'blur' }
          ],
          enddate: [
            { validator: validRules.isNumber, message: '结束时间只能是数字', trigger: 'blur' }
          ],
        },
        searchParams: {
          chdrcoy: '',
          agntnum: '',
        },
        searchRules: {
          chdrcoy: [
            { validator: validRules.isEnglishOrNumber, message: '请正确输入公司！', trigger: 'blur' }
          ],
          agntnum: [
            { validator: validRules.isEnglishOrNumber, message: '请正确输入营销员代码！', trigger: 'blur' }
          ]
        },
        tableData: [],
        loading: false,

        form: {
          chdrcoy: '',
          branch: '',
          agcyname: '',
          agntnum: '',
          agntname: '',
          vldstrdate: '',
          vldenddate: '',
          strdate: '',
          enddate: '',
          crtuser: '',
          chaguser: '',
          jobName: '',
          userProfile: '',
          method: '',
        },
        userInfo: {},
        option: '',
        options: [
          { value: '1', label: '测试1' },
          { value: '2', label: '测试2' }
        ],

        //需要给分页组件传的信息
        paginations: {
          total: 0,        // 总数
          pageIndex: 1,  // 当前位于哪页
          pageSize: 10,   // 1页显示多少条
          pageSizes: [5, 10, 15, 20],  //每页显示多少条
          layout: "total, sizes, prev, pager, next, jumper"   // 翻页属性
        },
        pickerOptions1: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          }
        }
      }
    },
    created() {
      //          this.getList();
      this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
    },
    mounted() {
      // this.getList();
    },
    methods: {
      modifyClick(row) {
        if (this.$refs['form']) {
          this.$refs['form'].resetFields();
        }
        this.title_O = '绩优代理人维护编辑';
        this.showDialog = true;
        Object.assign(this.form, row);
        this.form.method = 'modify';
      },
      addClick() {
        if (this.$refs['form']) {
          this.$refs['form'].resetFields();
          this.form = {
            chdrcoy: '',
            branch: '',
            agcyname: '',
            agntnum: '',
            agntname: '',
            vldstrdate: '',
            vldenddate: '',
            strdate: '',
            enddate: '',
            crtuser: '',
            chaguser: '',
            jobName: '',
            userProfile: '',
            method: '',
          }
        }
        this.$set(this.form, 'vldstrdate', '0');
        this.$set(this.form, 'vldenddate', '0');
        this.$set(this.form, 'strdate', '0');
        this.$set(this.form, 'enddate', '0');
        this.title_O = '绩优代理人维护新增';
        this.showDialog = true;
        this.form.crtuser = this.userInfo.account;
        this.form.method = 'add';
      },
      viewClick(row) {
        if (this.$refs['form']) {
          this.$refs['form'].resetFields();
        }
        this.title_O = '绩优代理人维护查看';
        this.showDialog = true;
        Object.assign(this.form, row);
        this.form.method = 'view';
      },
      checkAgntnum() {
        if (this.form.agntnum) {
          let para = {
            agntnum: this.form.agntnum
          }
          let thisF = this;
          api.checkAgntnum(para).then(res => {
            if (res.resultCode == constant.SUCCESS) {
              thisF.form.agcyname = res.data.agcyname;
              thisF.form.agntname = res.data.surname;
            }
          }).catch(err => {
            console.log(err);
          })
        }
      },
      // 查询列表
      getList() {
        this.$refs['searchForm'].validate((valid) => {
          if (valid) {
            this.paginations.total = 0;
            this.tableData = [];
            let para = {
              "chdrcoy": this.searchParams.chdrcoy,
              "agntnum": this.searchParams.agntnum,
              "pageSize": this.paginations.pageSize,
              "pageNumber": this.paginations.pageIndex,
            }

            this.$store.dispatch('loading/CHANGE_LOADING', true)

            api.queryList(para).then(res => {
              this.$store.dispatch('loading/CHANGE_LOADING', false)
              if (res.resultCode == constant.SUCCESS) {
                if (res.data && res.data.list.length) {
                  this.paginations.total = res.data.total;
                  this.tableData = res.data.list;
                } else {
                  this.$message({ message: '无符合条件任务！', type: 'error' });
                }
              }
            }).catch(err => {
              this.$store.dispatch('loading/CHANGE_LOADING', false);
            })
          }
        });
      },
      saveData() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.$store.dispatch('loading/CHANGE_LOADING', true)
            api.editData(this.form).then(res => {
              this.$store.dispatch('loading/CHANGE_LOADING', false)
              if (res.resultCode == constant.SUCCESS) {
                this.showDialog = false;
                this.searchParams.chdrcoy = this.form.chdrcoy;
                this.searchParams.agntnum = this.form.agntnum;
                this.getList();
              }
            }).catch(err => {
              this.$store.dispatch('loading/CHANGE_LOADING', false);
            })
          }
        });
      },
      deleteData(row) {
        this.$confirm('确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('loading/CHANGE_LOADING', true)
          let para = {
            agntnum: row.agntnum
          }
          api.deleteData(para).then(res => {
            this.$store.dispatch('loading/CHANGE_LOADING', false)
            if (res.resultCode == constant.SUCCESS) {
              this.getList();
            }

          }).catch(err => {
            this.$store.dispatch('loading/CHANGE_LOADING', false);
          })
        }).catch(() => {

        });

      },
      // 每页多少条切换
      handleSizeChange(pageSize) {
        this.paginations.pageSize = pageSize;
        this.getList();
      },
      // 上下分页
      handleCurrentChange(page) {
        this.paginations.pageIndex = page;
        this.getList();
      },
      // 根据条件查询列表
      subGetList() {
        this.paginations.pageIndex = 1;
        this.getList();
      },
      // 清除查询条件
      resetSearch() {
        this.searchParams.chdrcoy = '';
        this.searchParams.agntnum = '';
      }
    },
    computed: {
      ...mapGetters(['tags']),
      reload() {
        return this.$store.state.loading.reload;
      }
    },
    activated() {
      if (this.reload) {
        this.$store.dispatch('loading/CHNAGE_RELOAD', false)
        Object.assign(this.$data, this.$options.data());
      }
    }
  }

</script>

<style lang="less" scoped>
  .fillcontain {
    padding-bottom: 0;
  }

  .contain {
    background: #fff;
    padding: 10px;
    margin-bottom: 20px;
  }

  .pagination {
    padding: 10px 20px;
    text-align: right;
  }

  .operationIcon {
    // padding: 12px;
    border: 1px solid #e5e5e5;
    border-radius: 50%;
    width: 22px;
    height: 22px;
    display: inline-block;
    cursor: pointer;
    box-shadow: 0 0 3px #f9f9f9;
  }

  .search_container {
    padding: 20px 20px 0 20px;
    //    border-top: 1px solid #e1e1e1;
  }

  .common-box {
    padding-bottom: 15px;
  }

  .common-lab {
    line-height: normal !important;
    text-align: right;

    .labelName {
      text-align: right;
    }
  }
</style>